import socket
import struct
import time
import sys

class Client(object):
    def __init__(self, port: int = 9001):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.addr = '', port
        self.socket.bind(self.addr)

    def run(self):
        try:
            self.socket.connect(('', 8000))
            data = struct.pack('!iii', 0, 200, 24)
            self.socket.send(data)
            while True:
                data = self.socket.recv(1024)
                if data:
                    print('recv:', data.decode())
                    time.sleep(5)
                    data = str(int(data.decode()) + 1)
                    self.socket.send(data.encode())
                else:
                    print('server dosconnect!')
                    break
        except Exception as e:
            self.socket.close()
            print(e)

    def __del__(self):
        self.socket.close()


if __name__ == '__main__':
    print(sys.argv)
    cli = Client(port = int(sys.argv[1]))
    cli.run()